package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.annotation.LoginUser;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.dto.StockOrderDTO;
import io.renren.dto.StockOrdermxDTO;
import io.renren.dto.TemporaryOrderDTO;
import io.renren.entity.UserEntity;
import io.renren.service.StockOrderService;
import io.renren.service.StockOrdermxService;
import io.renren.service.SysDeptService;
import io.renren.service.TemporaryOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.reflect.Array;
import java.util.*;

import static org.bouncycastle.asn1.cmc.CMCStatus.pending;


/**
 * 食材采购表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@RestController
@RequestMapping("api/stockorder")
@Api(tags="食材采购")
public class StockOrderController {
    @Autowired
    private StockOrderService stockOrderService;
    @Autowired
    private StockOrdermxService stockOrdermxService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private TemporaryOrderService temporaryOrderService;

    @GetMapping("getChedOrder")
    @ApiOperation("主厨订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> getChedOrder(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<StockOrderDTO> page = stockOrderService.getOrder(params, entity);
        TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(0, entity.getId(), entity.getDeptId());
        ArrayList<StockOrderDTO> arraylist = new ArrayList<>();
        Page page1 = (Page)params.get("page");
        if(1 == page1.getCurrent() && null != temOrder){
            StockOrderDTO stockOrderDTO = new StockOrderDTO();
            stockOrderDTO.setCreateDate(temOrder.getCreateTime());
            stockOrderDTO.setMealTime(temOrder.getMealTime());
            stockOrderDTO.setFlag(1);
            String deptName = sysDeptService.getDeptName(entity.getDeptId()).getName()+"每日进货清单草稿";
            stockOrderDTO.setChedRemark(deptName);
            arraylist.add(stockOrderDTO);
        }
        // String 根据用户的部门id，取部门名称，+“食材进货清单” TODO: 2019/12/12
        HashMap<String, Object> map = new HashMap<>();
        for (StockOrderDTO stockOrderDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(stockOrderDTO.getCanteenId()).getName()+"每日进货清单";
            stockOrderDTO.setChedRemark(deptName);
            arraylist.add(stockOrderDTO);
        }
        page.setList(arraylist);
        // 返回待处理的订单的数量 TODO: 2019/12/12
        StockOrderDTO total = stockOrderService.getTotal(entity.getId(), null, entity.getDeptId(), 4);
        map.put("list",page);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }
    @GetMapping("home")
    @ApiOperation("食材采购列表")
    public Result<PageData<StockOrderDTO>> dblist(){
        Map<String, Object> params =new HashMap<>();
        List<StockOrderDTO> list = stockOrderService.home(params);

        return new Result().ok(list);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<StockOrderDTO> get(@PathVariable("id") Long id){
        StockOrderDTO data = stockOrderService.get(id);

        return new Result<StockOrderDTO>().ok(data);
    }


    @GetMapping("pageAdmin")
    @ApiOperation("食堂管理员分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> pageAdmin(@ApiIgnore @RequestParam Map<String, Object> params,@LoginUser UserEntity entity){
        PageData<StockOrderDTO> page = stockOrderService.pageAdmin(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        for (StockOrderDTO stockOrderDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(stockOrderDTO.getCanteenId()).getName()+"每日进货清单";
            stockOrderDTO.setChedRemark(deptName);
        }
        map.put("list",page);
        StockOrderDTO total = stockOrderService.getTotal(null, null, null, 1);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }

    @GetMapping("pagesTorehouseClerk")
    @ApiOperation("仓储员分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> pagesTorehouseClerk(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<StockOrderDTO> page = stockOrderService.pagesTorehouseClerk(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        for (StockOrderDTO stockOrderDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(stockOrderDTO.getCanteenId()).getName()+"每日进货清单";
            stockOrderDTO.setChedRemark(deptName);
        }
        // 返回待处理的订单的数量 TODO: 2019/12/12
        map.put("list",page);
        StockOrderDTO total = stockOrderService.getTotal(null, null,entity.getDeptId(), 3);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }

    @PostMapping("pageAdminUpdate")
    @ApiOperation("食堂管理员审核")
    public Result updateExam(@RequestBody StockOrderDTO dto, @LoginUser UserEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        return  stockOrderService.updateExamine(dto,entity);
    }
    /**
     * 食材采购订单生成列表
     * @return
     */
    @PostMapping("saveOrder")
    @ApiOperation("订单生成列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
    })
    public Result saveOrder(@RequestBody JSONObject params, @LoginUser UserEntity entity){
        stockOrderService.saveOrder(params,entity);
        return new Result();
    }
    /**
     * 根据供应商id获取订
     * @param supplierId
     * @return
     */
    @GetMapping("/selecSupplierOd")
    @ApiOperation("供应商订单信息")
    public Result<Map> selecSupplierOd(Long supplierId){
        Map map = stockOrderService.selecSupplierOd(supplierId);

        return new Result<Map>().ok(map);
    }

    /**
     * 根据供应商id和订单id获取订单详情
     * @param stockOrderId
     * @return
     */
    @GetMapping("/selecSupplierOrder1")
    @ApiOperation("供应商订单详情信息")
    public Result<List<StockOrdermxDTO>> selecSupplierOrder(Long supplierId, Long  stockOrderId ){
        List<StockOrdermxDTO> data = stockOrdermxService.selecSupplierOrder1(supplierId,stockOrderId);

        return new Result<List<StockOrdermxDTO>>().ok(data); //
    }

    @PostMapping("UpdateSupplierStutas")
    @ApiOperation("供应商修改订单")
    public Result UpdateSupplierStutas(@RequestBody StockOrderDTO dto, @LoginUser UserEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);
        return  stockOrderService.UpdateSupplierStutas(dto, entity);
    }
    /**
     * 保存实际用餐人数
     * @return
     */
    @PostMapping("updateNum")
    @ApiOperation("修改人数")
    public Result updateNum(@RequestBody StockOrderDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);
        StockOrderDTO stockOrderDTO = stockOrderService.get(dto.getStockOrderId());
        dto.setStatus(stockOrderDTO.getStatus());
        stockOrderService.update(dto);

        return new Result();
    }
}